home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Essentials / Developer Essentials Nov 90 / Apple II / Apple.II.partition / Tools / Technical.Notes / IIGS / TN.IIGS.090 < prev    next >
Encoding:
Text File  |  1990-09-21  |  5.5 KB  |  121 lines  |  [TEXT/pdos]

  1. Apple II
  2. Technical Notes
  3. _____________________________________________________________________________
  4.                                                   Developer Technical Support
  5.  
  6. Apple IIgs
  7. #90:    65816 Tips and Pitfalls
  8.  
  9. Written by:    Dave Lyons                                      September 1990
  10.  
  11. This Technical Note presents short 65816 assembly language examples 
  12. illustrating pitfalls and clever techniques.
  13. _____________________________________________________________________________
  14.  
  15.  
  16. Dispatching Through an Address Table
  17.  
  18. The 65816 has a JSR ($aaaa,X) instruction for calling a selected subroutine 
  19. from a table of addresses, but it has no JSL ($aaaa,X) instruction.  If you 
  20. need to dispatch to one of several routines that are not all in the same bank, 
  21. you need an approach like the following.  The idea is to push a three-byte 
  22. "RTL address" on the stack and then preform an RTL to it.
  23.  
  24.        asl a                ;take routine number in A and
  25.        asl a                ; multiply it by 4
  26.        tax                  ;put table index into X
  27.        lda table+1,x        ;get "middle" word of address
  28.        pha                  ; and push it
  29.        lda table,x          ;get low word and
  30.        dec a                ; decrement it by one
  31.        phb                  ;push a single throw-away byte
  32.        sta 1,s              ;store over low two of the 3 bytes
  33.        rtl                  ;transfer control to the routine
  34. table  dc.l routine1        ;table of 4-byte subroutine addresses
  35.        dc.l routine2
  36.        dc.l routine3
  37.        ...
  38.  
  39. This code is correct because RTL pulls three bytes off the stack and 
  40. increments the two low bytes without incrementing the high byte.
  41.  
  42. On the other hand, the following code is not correct.  The approach here is to 
  43. make a table of addresses minus one.
  44.  
  45.            asl a             W
  46.            asl a             R  ;multiply index by 4
  47.            tax               O  ; and put it in X
  48.            lda table+1,x     N  ;get the "middle" word
  49.            pha               G  ; and push it
  50.            lda table,x       !  ;get the low word
  51.            phb               W  ;push a single throw-away byte
  52.            sta 1,s           R  ;store over low two bytes
  53.            rtl               O  ;transfer control to the routine
  54.     table  dc.l routine1-1   N  ;table of 4-byte addresses minus one
  55.            dc.l routine2-1   G
  56.            dc.l routine3-1   !
  57.            ...
  58. This second sample code fragment fails if any of the routines in the table 
  59. comes at the first byte of a bank.  For example, if routine1 is at $060000, 
  60. the address pushed is $05FFFF, and RTL transfers control to $050000, not 
  61. $060000.
  62.  
  63. Dereferencing Handles Without Direct Page Space
  64.  
  65. When your code gets called with the D register undefined, you must not use 
  66. direct page addressing without setting D to a known good value.  Preserving 
  67. and restoring locations on the caller's direct page is not reliable, because D 
  68. could be pointing at bytes below the stack pointer (which can be destroyed by 
  69. interrupts) or even at the $C0xx soft switches (that would make your direct 
  70. page accesses accidentally fiddle with hardware).
  71.  
  72. A common way to get temporary direct page space is to point D at part of your 
  73. stack.  This following code dereferences a handle stored in the A and X 
  74. registers (if the handle is $E01234 and refers to a block of memory at 
  75. $056789, then on entry A=$00E0 and X=$1234, and on exit A=$0005 and X=$6789).
  76.  
  77.        phd                  ;save caller's direct-page register
  78.        pha                  ;push high word of handle
  79.        phx                  ;push low word of handle
  80.        tsc                  ;get stack pointer in A
  81.        tcd                  ;and put it in D
  82.        lda [1]              ;get low word of master pointer (no ",Y"!)
  83.        tax                  ; and put it in X
  84.        ldy #$0002           ;offset to high word of master pointer
  85.        lda [1],y            ;get high word
  86.        ply                  ;remove low word of handle
  87.        ply                  ; and high word
  88.        pld                  ;restore the caller's direct-page register
  89.  
  90. Direct page addressing isn't the only way to address through pointers.  Here's 
  91. the same routine as above, but using the Data Bank register (B) instead of 
  92. fiddling with D.  (Note that handles do not have to be in bank $E0 or $E1, 
  93. although they usually are.)
  94.  
  95.        phb                  ;save caller's data bank register
  96.        xba                  ;swap bytes in the high word
  97.        pha                  ;push swapped bytes on stack
  98.        plb                  ;pull the original high byte of the low word
  99.        plb                  ;sets B to the bank byte of the pointer
  100.        lda $0002,x          ;load the high word of the master pointer
  101.        pha                  ; and save it on the stack
  102.        lda $0000,x          ;load the low word of the master pointer
  103.        tax                  ;and return it in X
  104.        pla                  ;restore the high word in A
  105.        plb                  ;restore the caller's data bank register
  106.  
  107. Emulation Mode Has 65816 Features
  108.  
  109. You don't have to switch into Native mode just to do an eight-bit operation 
  110. with long addressing.  Most 65816-specific instructions and addressing modes 
  111. work in emulation mode in approximately the same way they work in eight-bit 
  112. native mode.  See the "Further Reference" for details.
  113.  
  114.  
  115. Further Reference
  116. _____________________________________________________________________________
  117.   o  Apple IIgs Hardware Reference
  118.   o  Programming the 65816, Including the 6502, 65C02 and 65802 
  119.      (Eyes and Lichty, 1986, Brady)
  120.  
  121.